<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增字典分组</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form layui-form-pane" action="" name="frmMain">
    <input type="hidden" name="enable" value="1">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item ">
                <label class="layui-form-label">字典名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入字典名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item ">
                <label class="layui-form-label">字典描述</label>
                <div class="layui-input-block">
                    <input type="text" name="notes" autocomplete="off" placeholder="请输入字典描述"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../config/apiUrl.js"></script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery','http','popup','common','toast'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let http = layui.http;
        let popup = layui.popup;
        let common = layui.common;
        let toast = layui.toast;
        let res_id = common.getUrlParams("id");

        function closeWin(){
            let layer_index = parent.layer.getFrameIndex(window.name);
            if(layer_index>0){
                parent.layer.close(layer_index); //关闭当前页
            }
        }
        $(document).keyup(function (event) {
            if (event.keyCode == 27) {
                closeWin();
            }
        });

        form.on('submit(user-save)', function(data) {
            layer.confirm('确定要新增字典么', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                let data_post = data.field;
                data_post.pid = res_id;
                http.ajax({
                    url: apiUrlConfig.dicGroup.add,
                    dataType: 'json',
                    type: 'post',
                    data: JSON.stringify(data_post),
                    contentType: "application/json",
                    success: function (result) {
                        layer.close(loading);
                        if (result.status == 200) {
                            parent.layui.toast.success({title: false,message: '保存成功'})
                            const bc = new BroadcastChannel('dictListReload');
                            bc.postMessage("dictListReload");
                            closeWin();
                        } else {
                            if (result.status == 10001) {
                                //字段提示
                                let errorMsg = JSON.parse(result.msg);
                                for (let key in errorMsg) {
                                    layer.tips(errorMsg[key], $("[name='"+key+"']").parent(), {
                                        tips: [3,'#f56c6c'],time:5000,tipsMore: true
                                    });
                                    console.error(key,errorMsg[key])
                                }
                            } else {
                                toast.error({title: false,message: result.msg})
                            }
                        }
                    },
                    error: function (res) {
                        layer.close(loading);
                    }
                })
            });
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>
